<?php

use yii\helpers\Html;
use yii\helpers\Url;
use kartik\grid\GridView;
use backend\models\Country;
use backend\assets\KartikAsset;
use backend\models\ProjectSingleDjr;

/* @var $this yii\web\View */
/* @var $searchModel backend\models\ProjectSingleSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '特殊项目库';
$this->params['breadcrumbs'][] = ['label' => '特殊项目库', 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => '我上传的', 'url' => ['index', 'ProjectSingleSearch[owner]' => Yii::$app->user->id]];
$this->params['breadcrumbs'][] = $this->title;

KartikAsset::register($this);

$importJs = <<<IMPORT
    $(document).ready(function () {
        $("#project_file").fileinput({
                browseLabel:'选择文件',
                language:'zh',
                showBrowse:false,
                showCaption: false,
                dropZoneTitle:'点击或拖拽文件到这里',
                dropZoneClickTitle:'',           
                allowedFileExtensions: ["xls", "xlsx"],
                previewFileIconSettings: {
                    'xls': '<i class="fa fa-file-excel-o text-success"></i>',              
                },
                previewFileExtSettings: {              
                    'xls': function(ext) {
                        return ext.match(/(xls|xlsx)$/i);
                    },              
                },
                browseOnZoneClick: true,
                uploadAsync: true,
                showUpload: true, 
                showRemove: true, 
                showUploadedThumbs: true,
                uploadUrl: $("#import-project-url").val(),
                deleteUrl: "",
    
            }).on('fileloaded', function(event, file, previewId, index, reader) {
    
            }).on('filebatchselected', function(event, files) {
                // console.log(files);
            })
    })
IMPORT;
$js = <<<JS
  $('[data-toggle="tooltip"]').tooltip()
JS;

$this->registerJs($js);
$this->registerJs($importJs);
echo '<input type="hidden" id="import-project-url" value="' . Url::to(['project-single/import']) . '">';

$css = <<<CSS
    .btn-primary {
        color: #fff;
        background-color: #2194f1;
        border-color: #2194f1;
    }
    .panel-primary > .panel-heading {
        color: #fff;
        background-color: #2194f1;
        border-color: #2194f1;
    }
CSS;
$this->registerCss($css);

?>

<!-- Modal -->
<div class="modal fade" id="importExcelModal" tabindex="-1" role="dialog" aria-labelledby="importExcelModalLabel"
     aria-hidden="true">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="importExcelModalLabel">导入资产包</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div class="file-loading">
                    <input id="project_file" name="project_file" type="file"'>
                </div>
                <div id="kartik-file-errors"></div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal"
                        onclick="javascript:window.location.reload();">关闭
                </button>
            </div>
        </div>
    </div>
</div>

<div class="project-single-index">
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>
    <?php

    $columns = [
        ['class' => 'yii\grid\SerialColumn'],
        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'level',
            'readonly' => function ($model, $key, $index, $widget) {
                return !($model->owner == Yii::$app->user->id || in_array(Yii::$app->user->identity->realname, Yii::$app->params['package_ease_personal']));
            },
            'value' => function ($model) {
                return Yii::$app->params['project_single_level'][$model->level];
            },
            'filterType' => GridView::FILTER_SELECT2,
            'filter' => Yii::$app->params['project_single_level'],
            'filterWidgetOptions' => [
                'pluginOptions' => ['allowClear' => true],
            ],
            'filterInputOptions' => ['placeholder' => '等级'],
            'format' => 'raw',
            'editableOptions' => [
                'asPopover' => true,
                'inputType' => \kartik\editable\Editable::INPUT_WIDGET,
                'widgetClass' => \kartik\editable\Editable::INPUT_SELECT2,
                'options' => [
                    'data' => Yii::$app->params['project_single_level'],
                ],
            ],
        ],
        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'title',
            'readonly' => function ($model, $key, $index, $widget) {
                return !($model->owner == Yii::$app->user->id || in_array(Yii::$app->user->identity->realname, Yii::$app->params['package_ease_personal']));
            },
            'editableOptions' => [
                'asPopover' => true,
                'inputType' => \kartik\editable\Editable::INPUT_TEXT,
            ],
        ],
        [
            'attribute' => 'province',
            'vAlign' => 'middle',
            'hAlign' => 'middle',
            'value' => function ($model) {
                return Country::findOne($model->province)->title;
            },
            'filterType' => GridView::FILTER_SELECT2,
            'filter' => Country::localCity('`pid`=0 AND `order`>0'),
            'filterWidgetOptions' => [
                'pluginOptions' => ['allowClear' => true],
            ],
            'filterInputOptions' => ['placeholder' => '省份'],
            'format' => 'raw',
        ],
        [
            'attribute' => 'city',
            'vAlign' => 'middle',
            'value' => function ($model) {
                if ($model->city) {
                    $modelCountry = new Country();
                    $city = implode(',', $modelCountry->getProvinces("id in ($model->city)"));
                } else {
                    $city = '';
                }
                return $city;
            },
            'filterType' => GridView::FILTER_SELECT2,
            'filter' => $searchModel->province ? Country::localCity("`pid`=" . $searchModel->province) : [],
            'filterWidgetOptions' => [
                'pluginOptions' => ['allowClear' => true],
            ],
            'filterInputOptions' => ['placeholder' => '城市'],
            'format' => 'raw'

        ],
        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'category',
            'readonly' => function ($model, $key, $index, $widget) {
                return !($model->owner == Yii::$app->user->id || in_array(Yii::$app->user->identity->realname, Yii::$app->params['package_ease_personal']));
            },
            'value' => function ($model) {
                return Yii::$app->params['project_single_category'][$model->category];
            },
            'filterType' => GridView::FILTER_SELECT2,
            'filter' => Yii::$app->params['project_single_category'],
            'filterWidgetOptions' => [
                'pluginOptions' => ['allowClear' => true],
            ],
            'filterInputOptions' => ['placeholder' => '选择类别'],
            'format' => 'raw',
            'editableOptions' => [
                'asPopover' => true,
                'inputType' => \kartik\editable\Editable::INPUT_WIDGET,
                'widgetClass' => \kartik\editable\Editable::INPUT_SELECT2,
                'options' => [
                    'data' => Yii::$app->params['project_single_category'],
                ],
            ],
        ],
        [
            'attribute' => 'tag',
            'format' => 'raw',
            'value' => function ($model) {
                $arr_tag = explode(',', $model->tag);
                $arr_tag_tag = [];
                if (is_array($arr_tag) && count($arr_tag) > 0) {
                    foreach ($arr_tag as $item) {
                        $arr_tag_tag[] = \backend\models\Tags::findOne(['id' => $item])->title;
                    }
//                        return "<span title='" . implode(',', $arr_tag_tag) . "' data-toggle='tooltip' data-placement='top'>" . mb_strcut(implode(',', $arr_tag_tag), 0, 30) . "</span>";
                    return implode(',', $arr_tag_tag);
                } else {
                    return;
                }
            },
            'filterType' => GridView::FILTER_SELECT2,
            'filter' => \backend\models\Tags::getAllTags(),
            'filterWidgetOptions' => [
                'pluginOptions' => ['allowClear' => true, 'multiple' => false],
            ],
            'filterInputOptions' => ['placeholder' => '选择标签'],
        ],
        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'detail',
            'format' => 'raw',
            'value' => function ($model) {
                return "<span title='" . $model->detail . "' data-toggle='tooltip' data-placement='top'>" . mb_strcut($model->detail, 0, 30) . "</span>";
            },
            'readonly' => function ($model, $key, $index, $widget) {
                return !($model->owner == Yii::$app->user->id || in_array(Yii::$app->user->identity->realname, Yii::$app->params['package_ease_personal']));
            },
            'editableOptions' => [
                'asPopover' => true,
                'inputType' => \kartik\editable\Editable::INPUT_TEXTAREA,
            ],

        ],
        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'asset',
            'format' => 'raw',
            'value' => function ($model) {
                return "<span title='" . $model->asset . "' data-toggle='tooltip' data-placement='top'>" . mb_strcut($model->asset, 0, 30) . "</span>";
            },
            'readonly' => function ($model, $key, $index, $widget) {
                return !($model->owner == Yii::$app->user->id || in_array(Yii::$app->user->identity->realname, Yii::$app->params['package_ease_personal']));
            },
            'editableOptions' => [
                'asPopover' => true,
                'inputType' => \kartik\editable\Editable::INPUT_TEXTAREA,
            ],

        ],
        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'price',
            'readonly' => function ($model, $key, $index, $widget) {
                return !($model->owner == Yii::$app->user->id || in_array(Yii::$app->user->identity->realname, Yii::$app->params['package_ease_personal']));
            },
            'format' => 'raw',
            'value' => function ($model) {
                return "<span title='" . Yii::$app->format->formatCurrency($model->price) . "' data-toggle='tooltip' data-placement='top'>" . Yii::$app->formatter->asDecimal($model->price) . "</span>";
            },
            'editableOptions' => [
                'asPopover' => true,
                'inputType' => \kartik\editable\Editable::INPUT_TEXT,
            ],
        ],
        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'highlights',
            'format' => 'raw',
            'value' => function ($model) {
                return "<span title='" . $model->highlights . "' data-toggle='tooltip' data-placement='top'>" . mb_strcut($model->highlights, 0, 30) . "</span>";
            },
            'readonly' => function ($model, $key, $index, $widget) {
                return !($model->owner == Yii::$app->user->id || in_array(Yii::$app->user->identity->realname, Yii::$app->params['package_ease_personal']));
            },
            'editableOptions' => [
                'asPopover' => true,
                'inputType' => \kartik\editable\Editable::INPUT_TEXTAREA,
            ],
        ],
        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'contact',
            'format' => 'raw',
            'value' => function ($model) {
                return "<span title='" . $model->contact . "' data-toggle='tooltip' data-placement='top'>" . mb_strcut($model->contact, 0, 30) . "</span>";
            },
            'readonly' => function ($model, $key, $index, $widget) {
                return !($model->owner == Yii::$app->user->id || in_array(Yii::$app->user->identity->realname, Yii::$app->params['package_ease_personal']));
            },
            'editableOptions' => [
                'asPopover' => true,
                'inputType' => \kartik\editable\Editable::INPUT_TEXTAREA,
            ],
        ],
        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'history',
            'format' => 'raw',
            'value' => function ($model) {
                return "<span title='" . $model->history . "' data-toggle='tooltip' data-placement='top'>" . mb_strcut($model->history, 0, 30) . "</span>";
            },
            'readonly' => function ($model, $key, $index, $widget) {
                return !($model->owner == Yii::$app->user->id || in_array(Yii::$app->user->identity->realname, Yii::$app->params['package_ease_personal']));
            },
            'editableOptions' => [
                'asPopover' => true,
                'inputType' => \kartik\editable\Editable::INPUT_TEXTAREA,
            ],
        ],

        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'follow',
            'format' => 'raw',
            'value' => function ($model) {
                return "<span title='" . $model->follow . "' data-toggle='tooltip' data-placement='top'>" . mb_strcut($model->follow, 0, 30) . "</span>";
            },
            'readonly' => function ($model, $key, $index, $widget) {
                return !($model->owner == Yii::$app->user->id || in_array(Yii::$app->user->identity->realname, Yii::$app->params['package_ease_personal']));
            },
            'editableOptions' => [
                'asPopover' => true,
                'inputType' => \kartik\editable\Editable::INPUT_TEXTAREA,
            ],
        ],
        [
            'attribute' => 'owner',
            'value' => function ($model) {
                return $model->publisher->realname;
            },
            'filterType' => GridView::FILTER_SELECT2,
            'filter' => \backend\models\User::getActiveUsers(),
            'filterWidgetOptions' => [
                'pluginOptions' => ['allowClear' => true],
            ],
            'filterInputOptions' => ['placeholder' => '上传人'],
            'format' => 'raw',
        ],
        [
            'attribute' => 'updated_at',
            'value' => function ($model) {
                if ($model->updated_at > 0) {
                    return date('Y-m-d', strtotime($model->updated_at));
                } else {
                    return '';
                }
            }
        ],
        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'source',
            'format' => 'raw',
            'value' => function ($model) {
                return "<span title='" . $model->source . "' data-toggle='tooltip' data-placement='top'>" . mb_strcut($model->source, 0, 30) . "</span>";
            },
            'readonly' => function ($model, $key, $index, $widget) {
                return !($model->owner == Yii::$app->user->id || in_array(Yii::$app->user->identity->realname, Yii::$app->params['package_ease_personal']));
            },
            'editableOptions' => [
                'asPopover' => true,
                'inputType' => \kartik\editable\Editable::INPUT_TEXTAREA,
            ],
        ],
        ['class' => 'yii\grid\ActionColumn', 'template' => '{update} {delete} {publish}',
            'buttons' => [
                'delete' => function ($url, $model, $key) {
                    return $model->owner == Yii::$app->user->id ? Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [
                        'data-confirm' => '确定要删除吗？',
                        'data-method' => 'post',
                    ]) : '';
                },
                'publish' => function ($url, $model, $key) use ($projectSingleDjrModel, $projectDjr, $projectMappingDjr, $projectSingleAssetDjr, $projectFileDjr) {
                    /*
                    echo '<div class="modal fade" id="publish' . $key . '" tabindex="-1" role="dialog">
                            <div class="modal-dialog modal-lg" role="document">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <h5 class="modal-title" id="publish2djr' . $key . '">发布到点金人</h5>
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                            <span aria-hidden="true">&times;</span>
                                        </button>
                                    </div>
                                    <div class="modal-body">';
                    echo $this->render('_form_publish2djr', ['model' => $model, 'projectSingleDjrModel' => $projectSingleDjrModel, 'projectDjr' => $projectDjr, 'projectMappingDjr' => $projectMappingDjr, 'projectSingleAssetDjr' => $projectSingleAssetDjr, 'projectFileDjr' => $projectFileDjr,]);
                    echo '</div><div class="modal-footer"><button type = "button" class="btn btn-secondary" data-dismiss="modal" >关闭</button></div></div></div></div>';
                    return ($model->owner == Yii::$app->user->id && $model->is_published == 0) ?
                        Html::a('<span class="glyphicon glyphicon-share-alt" ></span > ', '', ['title' => '发布到点金人', 'data-toggle' => 'modal', 'data-target' => "#publish$key"]) : '已发布至DJR';
                    */
                    if ($model->owner == Yii::$app->user->id && $model->is_published == 0) {
                        return Html::a('<span class="glyphicon glyphicon-share-alt" ></span > ', ['publish2djr', 'id' => $model->id], ['title' => '发布到点金人', 'target' => '_blank']);
                    } elseif ($model->is_published == 1) {
                        return '已发布至DJR';
                    } else {
                        return '';
                    }


                }
            ],
        ],
    ];
    echo GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'condensed' => true,
        'hover' => true,
        'responsive' => false,
        'toolbar' => [
            [
                'content' =>
                    Html::a('<i class="glyphicon glyphicon-plus"></i>创建', ['create'], ['title' => '创建项目', 'class' => 'btn btn-primary',]) .
                    Html::a('<i class="glyphicon glyphicon-repeat"></i>重置', ['index'], ['class' => 'btn btn-default', 'title' => '重置列表']) .
                    Html::button('导入', ['class' => 'btn btn-info', 'title' => '导入项目', 'data-toggle' => 'modal', 'data-target' => '#importExcelModal']) .
                    Html::a('<i class="glyphicon glyphicon-file"></i>模板', ['download-template'], ['class' => 'btn btn-warning', 'title' => '导入项目模板'])
            ],
            '{toggleData}'
        ],
        'panel' => [
            'heading' => '<h3 class="panel-title"><i class="glyphicon glyphicon-book"></i>特殊项目库</h3>',
            'type' => 'primary',
            'footer' => '',
        ],
        'columns' => $columns,
    ]);
    ?>

</div>
